<?php
/**
 * 水印
 * @author qishunweb
 */
class Watermark{
	public $nimage;
	public $image_size;
	public $simage;
	public $file_path;
	public function  __construct($file_path)	{
		$this->file_path=$file_path;
		$this->image_size = getimagesize($this->file_path);
		$this->iinfo=getimagesize($this->file_path,$iinfo);
		$this->nimage =imagecreatetruecolor($this->image_size[0],$this->image_size[1]);
		switch ($this->iinfo[2])
		{
			case 1:
				$this->simage =imagecreatefromgif($this->file_path);
				break;
			case 2:
				$this->simage =imagecreatefromjpeg($this->file_path);
				break;
			case 3:
				$this->simage =imagecreatefrompng($this->file_path);
				break;
			case 6:
				$this->simage =imagecreatefromwbmp($this->file_path);
				break;
			default:
				exit;
		}
		imagecopy($this->nimage,$this->simage,0,0,0,0,$this->image_size[0],$this->image_size[1]);
	}
	/**
	 * 文字水印
	 * @param unknown 文字内容
	 */
	public function words($txt){
		$red=imagecolorallocate($this->nimage,255,0,0);
		   //水印字符串
		imagestring($this->nimage,0,0,$this->image_size[1]-15,$txt,$red);
		$this->end();
	}
	/**
	 * 图片水印
	 * @param unknown $img  水印路径
	 */
	public function img ($img){
		$suffix =  explode(".", $img);
		if ($suffix[1]=="png"){
			$simage1 =imagecreatefrompng($img);
		}
		else if ($suffix[1]=="gif"){
			$simage1 =imagecreatefromgif($img);
		}
		$size= getimagesize($img);
		imagecopy($this->nimage,$simage1,0,0,0,0,$size[0],$size[1]);
		imagedestroy($simage1);
		$this->end();
	}
	public function end(){
		switch ($this->iinfo[2])
		{
			case 1:
				imagejpeg($this->nimage, $this->file_path);
				break;
			case 2:
				imagejpeg($this->nimage, $this->file_path);
				break;
			case 3:
				imagepng($this->nimage, $this->file_path);
				break;
			case 6:
				imagewbmp($this->nimage, $this->file_path);
				break;
		}
		//覆盖原上传文件
		imagedestroy($this->nimage);
		imagedestroy($this->simage);
	}
}